package org.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.demo.entity.Notification;
import org.example.demo.entity.User;
import org.example.demo.service.NotificationService;
import org.example.demo.service.UserService;
import org.example.demo.util.AjaxResult;
import org.example.demo.util.ReturnMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("notification")
public class NotificationController {

    @Resource
    private NotificationService notificationService;

    @Resource
    private UserService userService;

    @RequestMapping("selectByUserId")
    public AjaxResult selectByUserId(@RequestParam("userId") Integer userId) {
        List<Notification> notifications = notificationService.list(new QueryWrapper<Notification>().eq("user_id", userId).orderByDesc("create_time"));

//        ReturnMap returnMap = new ReturnMap();
        for (Notification notification : notifications) {
            User user=userService.getById(notification.getAppId());
            notification.setNickName(user.getName());
            notification.setAvatarUrl(user.getAvatarUrl());

        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", notifications);
//        returnMap.setData(notifications);
        return AjaxResult.success(map);
    }


    @RequestMapping("readNotification")
    public AjaxResult readNotification(@RequestParam("id") Long id) {
        Notification notification = notificationService.getById(id);
        notificationService.updateById(notification);

        return AjaxResult.success();
    }
}